﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ShiXuLong.Model;
using ShiXuLong.Model.ShopType1;

namespace ShiXuLong.Service
{
    /// <summary>
    /// 商品管理类
    /// </summary>
    public class ShopManagement
    {
        ShoesEntities db = new ShoesEntities();

        /// <summary>
        /// 获取商品所有数据，多条件查询
        /// </summary>
        /// <returns></returns>
        public List<Shops> GetShop(int limit, int page, string shopName, string shopTypeName, out int count)
        {
            //var list = db.Shops.Where(p=>true).ToList();
            var list = from item in db.Shops.Where(p => true)
                       select new Shops
                       {
                           Id = item.Id,
                           ShopId = item.ShopId,
                           ShopName = item.ShopName,
                           ProductProfile = item.ProductProfile,
                           ShopTypeName = item.ShopType.ShopTypeName,
                           RecommendId = item.RecommendId,
                           Display = item.Display,
                           Shopimage = item.Shopimage,
                           Price = item.Price,
                           Number = item.Number,
                           GuideId = item.GuideId,
                           Status = item.Status
                       };
            if (!string.IsNullOrWhiteSpace(shopName))
            {
                list = list.Where(p => p.ShopName.Contains(shopName));
            }
            if (!string.IsNullOrWhiteSpace(shopTypeName))
            {
                list = list.Where(p => p.ShopTypeName == shopTypeName);
            }
            count = list.Count();
            return list.ToList();
        }
        /// <summary>
        /// 获取所有商品数据
        /// </summary>
        /// <returns></returns>
        public List<Shops> GetShop()
        {
            //var list = db.Shops.Where(p=>true).ToList();
            var list = from item in db.Shops.Where(p => true)
                       select new Shops
                       {
                           ShopId = item.ShopId,
                           ShopName = item.ShopName,
                           ProductProfile = item.ProductProfile,
                           ShopTypeName = item.ShopType.ShopTypeName,
                           RecommendId = item.RecommendId,
                           Display = item.Display,
                           Shopimage = item.Shopimage,
                           Price = item.Price,
                           Number = item.Number,
                           GuideId = item.GuideId,
                           Status = item.Status
                       };
            return list.ToList();
        }
        /// <summary>
        /// 推荐
        /// </summary>
        /// <returns></returns>
        public int UpShopStatus(Shop shop)
        {
            if (shop.Status == 0)
            {
                shop.Status = 1;
                db.Entry(shop);
                return db.SaveChanges();
            }
            else if (shop.Status == 1)
            {
                shop.Status = 0;
                db.Entry(shop);
                return db.SaveChanges();
            }
            return db.SaveChanges();
        }

        /// <summary>
        /// 根据id获取数据
        /// </summary>
        /// <param name="limit"></param>
        /// <param name="page"></param>
        /// <param name="shopName"></param>
        /// <param name="shopType"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<Shop> GetShopById(int id)
        {
            var list = db.Shops.Where(p => p.Id == id).ToList();

            return list;
        }
        public int AddShop(Shop shop)
        {
            try
            {
                db.Shops.Add(shop);
                return db.SaveChanges();
            }
            catch (Exception)
            {
                return 0;
            }
        }
        /// <summary>
        /// 编辑功能，失败
        /// </summary>
        /// <param name="shop"></param>
        /// <returns></returns>
        public int EditShop(Shop shop)
        {
            db.Entry(shop).State = System.Data.Entity.EntityState.Modified;
            int res = db.SaveChanges();

            return res;
        }
        public int DelShop(int id)
        {
            var shop = db.Shops.Where(p => p.Id == id).ToList();
            var shop1 = shop[0];
            db.Shops.Remove(shop1);
            var res = db.SaveChanges();
            return res;
        }
        /// <summary>
        /// 获取所有的商品类别
        /// </summary>
        /// <returns></returns>
        public List<ShopTypess> GetShopType()
        {
            var list = from item in db.ShopTypes.Where(p => true)
                       select new ShopTypess
                       {
                           Id = item.Id,
                           ShopTypeName = item.ShopTypeName
                       };
            return list.ToList();
        }

        public List<PhotosModel> GetImgData(int limit, int page, out int count, int Id)
        {
            var sp = from item in db.Photos.Where(p => p.ShopId == Id)
                     select new PhotosModel
                     {
                         Id = item.Id,
                         Img = item.PicUrl,
                         ProductName = item.Shop.ShopName,

                     };

            count = sp.Count();
            sp = sp.OrderBy(p => p.Id);
            sp = sp.Skip((page - 1) * limit).Take(limit);
            return sp.ToList();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public List<Photo> GetProId(int ids)
        {
            var imglist = db.Photos.Where(p => p.ShopId == ids).ToList();
            return imglist;
        }
        /// <summary>
        /// 向图片表添加数据的方法
        /// </summary>
        /// <param name="pic"></param>
        /// <returns></returns>
        public int ImgAdd(Photo pic)
        {
            db.Photos.Add(pic);
            return db.SaveChanges();
        }
        /// <summary>
        /// 编辑图片
        /// </summary>
        /// <param name="pro"></param>
        /// <returns></returns>
        public int XiuGaiImg(Photo pro)
        {

            try
            {
                db.Entry(pro).State = System.Data.Entity.EntityState.Modified;
                return db.SaveChanges();
            }
            catch (Exception ex)
            {

                throw;
            }

        }
        /// <summary>
        /// 删除方法
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public int DelProImg(int Id)
        {
            var data = db.Photos.SingleOrDefault(p => p.Id == Id);
            db.Photos.Remove(data);
            return db.SaveChanges();
        }

        public List<ProductTypeModel> ProductTypeSyes(int page, int limit, string spuName, out int count)
        {
            var lists = from c in db.ShopTypes.Where(p => true)
                        select new ProductTypeModel
                        {
                            id = c.Id,
                            supClassName = c.ShopTypeName,


                        };
            count = lists.Count();
            if (!string.IsNullOrWhiteSpace(spuName))
            {
                lists = lists.Where(p => p.supClassName.Contains(spuName));
            }
            lists = lists.OrderBy(p => p.id);
            lists = lists.Skip((page - 1) * limit).Take(limit);
            return lists.ToList();
        }
        public int ServiceTypeAdd(ShopType re)
        {
            db.ShopTypes.Add(re);
            return db.SaveChanges();
        }
        public ProductTypeModel GetInfo(int id)
        {
            var lists = from item in db.ShopTypes.Where(p => p.Id == id)
                        select new ProductTypeModel
                        {
                            id = item.Id,
                            supClassName = item.ShopTypeName,

                        };
            return lists.SingleOrDefault();
        }
        /// <summary>
        /// 修改方法
        /// </summary>
        /// <param name="product"></param>
        /// <returns></returns>
        public int TypeEdit(ShopType re)
        {
            db.Entry(re).State = System.Data.Entity.EntityState.Modified;
            return db.SaveChanges();
        }
        /// <summary>
        /// 根据ID删除当前行
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DelProductType(int id)
        {
            var data = db.ShopTypes.SingleOrDefault(p => p.Id == id);
            db.ShopTypes.Remove(data);
            return db.SaveChanges();
        }
    }
}
